package com.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.MMenu;
import com.component.domain.vo.MenuVo;
import com.component.service.MMenuService;
import com.system.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname MMenuApi
 * @Description TODO
 * @Date 2023/9/28 17:35
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MMenuApi {

    @Autowired
    private MMenuService mMenuService;

    @GetMapping("page")
    public JSONObject page(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "menuId",required = false)String menuId,
                           @RequestParam(value = "menuName",required = false)String menuName,
                           @RequestParam(value = "componentName",required = false)String componentName,
                           @RequestParam(value = "path",required = false)String path){
        IPage<MMenu> iPage=mMenuService.pageChild(pageNum, pageSize, menuId,menuName,componentName,path);
        return Result.successInfo(iPage);
    }

    @ApiOperation("获取父菜单")
    @GetMapping("getparentlist")
    public JSONObject getParentList(){
        List<MMenu> menuList=mMenuService.parentList();
        return Result.successInfo(menuList);
    }

    @ApiOperation("菜单添加")
    @PostMapping("addorupdate")
    public JSONObject addMenu(@RequestBody MenuVo menuVo){
        if(menuVo.getId()!=null){
            MMenu mMenu=mMenuService.getById(menuVo.getId());
            if(mMenu == null){
                return Result.errorInfo("菜单不存在");
            }
            if(menuVo.getMenuName()!=null){
                mMenu.setMenuName(menuVo.getMenuName());
            }
            if (menuVo.getMenuComponent()!=null){
                mMenu.setMenuComponent(menuVo.getMenuComponent());
            }
            if(menuVo.getMenuPath()!=null){
                mMenu.setMenuPath(menuVo.getMenuPath());
            }
            if(mMenu.getMenuLevel()==2){
                if(!menuVo.getPid().equals("0")){
                    mMenu.setPid(menuVo.getPid());
                }
            }
            if(menuVo.getMenuLevel()==1){
                if(menuVo.getActiveindex()!=null){
                    mMenu.setActiveindex(menuVo.getActiveindex());
                }
            }
            mMenuService.updateById(mMenu);
            return Result.successInfo("更新成功");
        }else {
            MMenu mMenu=new MMenu();
            if(menuVo.getPid().equals("0")){
                mMenu.setPid("0");
                mMenu.setMenuLevel(1);
                if(menuVo.getActiveindex()==null){
                    return Result.errorInfo("选中标识不得为空");
                }
                mMenu.setActiveindex(menuVo.getActiveindex());
            }else {
                MMenu parent=mMenuService.getById(menuVo.getPid());
                if(parent==null){
                    return Result.errorInfo("选中的父id不存在");
                }
                mMenu.setPid(menuVo.getPid());
                mMenu.setMenuLevel(2);
                if(menuVo.getMenuComponent()==null){
                    return Result.errorInfo("组件标识不得为空");
                }
                mMenu.setMenuComponent(menuVo.getMenuComponent());
                if(menuVo.getMenuPath()==null){
                    return Result.errorInfo("组件路径不得为空");
                }
                mMenu.setMenuPath(menuVo.getMenuPath());
            }
            mMenu.setMenuName(menuVo.getMenuName());
            mMenuService.save(mMenu);
            return Result.successInfo("新增成功");
        }
    }

}
